<template>
  <div class="app-container">
    <el-row>
      <el-button type="success" icon="el-icon-plus" @click="createFlowchart">新建流程</el-button>
    </el-row>
    <el-row :gutter="20" class="panel-group">
      <!-- v-for 的目的是通过遍历传递过来的 flowchartList 将其中的元素显示到当前页面上 然后点击对应的flowchart对从数据库中 读取对应的信息-->
      <el-col
        v-for="flowchart in flowchartList"
        :key="flowchart.id"
        :xs="12"
        :sm="12"
        :lg="6"
        class="card-panel-col"
      >
        <router-link :to="'/workbench/edit/' + flowchart.id">
          <el-card class="card-panel" shadow="hover">
            <div class="card-panel-icon-wrapper icon-tree-table">
              <svg-icon icon-class="tree-table" class-name="card-panel-icon" />
            </div>
            <div class="card-panel-description">
              <div class="card-panel-text">{{ flowchart.name }}</div>
              <div class="card-panel-time">
                <div>创建时间:</div>
                <div>
                  {{ new Date(flowchart.create_time).toLocaleString() }}
                </div>
                <div>最后修改时间:</div>
                <div>
                  {{ new Date(flowchart.update_time).toLocaleString() }}
                </div>
              </div>
              <el-button
                type="danger"
                class="card-button"
                plain
                @click.prevent="deleteFlowchart(flowchart.id)"
                >删除</el-button
              >
            </div>
          </el-card>
        </router-link>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'FlowtList',
  data() {
    return {
      flowchartList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      // axios
      //   .get('/api/datalab/flowchart')
      //   .then((res) => {
      //     this.flowchartList = res.data
      //   })
      //   .catch((err) => {
      //     console.log(err)
      //   })
      this.flowchartList = [{'id': 12, 'name': '123123414', 'create_time': "2021-03-30 09:58:19.842852", 'update_time': "2022-05-12 17:36:49.355669"}]
    },
    createFlowchart() {
      this.$prompt('请输入名称', '新建分析流程', {  //$prompt 的作用是弹出一个对话框 $message的作用是在页面弹出创建成功和创建失败的消息
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(async ({ value }) => {
          try {
            const response = await axios.post(
              '/api/datalab/flowchart',
              {
                name: value
              }
            )
            this.flowchartList.unshift(response.data) // unshift 的作用是在response.data 的开头添加元素
            this.$message({
              type: 'success',
              message: value + '创建成功'
            })
          } catch (err) {
            console.log(err)
            this.$message({
              type: 'error',
              message: '创建失败，请检查网络'
            })
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '取消创建'
          })
        })
    },
    async deleteFlowchart(id) {
      try {
        const response = await axios.delete(
          `/api/datalab/flowchart/${id}`
        )
        console.log(response)
        this.flowchartList = this.flowchartList.filter(
          (flowchart) => flowchart.id !== id
        )
        this.$message({
          type: 'success',
          message: '删除成功'
        })
      } catch (err) {
        console.log(err)
        this.$message({
          type: 'error',
          message: '删除失败，请检查网络'
        })
      }
    },
    test() {
      console.log('test')
    }
  }
}
</script>

<style lang="scss" scoped>
.panel-group {
  margin-top: 10px;

  .card-panel-col {
    margin-bottom: 32px;
  }

  .card-panel {
    height: 200px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #f4f4f4;
    border-color: rgba(0, 0, 0, 0.05);

    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }

      .icon-tree-table {
        background: #40c9c6;
      }
    }

    .icon-tree-table {
      color: #40c9c6;
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 48px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;

      .card-panel-text {
        width: 100px;
        line-height: 18px;
        // color: rgba(0, 0, 0, 0.45);
        overflow: hidden;
        font-size: 20px;
        margin-bottom: 12px;
      }

      .card-panel-time {
        width: 150px;
        font-size: 13px;
        color: #999;
      }

      .card-button {
        padding: 10px;
        float: right;
        font-size: 15px;
      }
    }
  }
}

@media (max-width: 685px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}
</style>
